package homework_java.eg;

public class Synchronize_Thread4 { 
	public static int  k = 100;
     public void m4t1() { 
          synchronized(this) { //���󻥳���//同步锁
               int i = 5; 
               while( i-- > 0) { 
                    System.out.println(Thread.currentThread().getName() + " : " + i); 
                    System.out.println(k--);
                    try { 
                         Thread.sleep(500); 
                    } catch (InterruptedException ie) { 
                    } 
               } 
          } 
     } 
     public synchronized void m4t2() { //����������
          int i = 5; 
          while( i-- > 0) { 
               System.out.println(Thread.currentThread().getName() + " : " + i); 
               System.out.println(k--);
               try { 
                    Thread.sleep(500); 
               } catch (InterruptedException ie) { 
               } 
          } 
     }
     //static synchronized������ʵ����ͬ��������i=5, i=10, i=15���ԱȽ��
     public static synchronized void m4t3() { //��̬����������
         int i = 15; 
         while( i-- > 0) { 
              System.out.println(Thread.currentThread().getName() + " : " + i); 
              System.out.println(k--);
              try { 
                   Thread.sleep(500); 
              } catch (InterruptedException ie) { 
              } 
         } 
    }
     public static void main(String[] args) { 
          final Synchronize_Thread4 myt2 = new Synchronize_Thread4(); 
          /*Thread t1 = new Thread(  new Runnable() {  
			  public void run() {  myt2.m4t1();  }  
			  }, "Thead1"  ); 
          Thread t2 = new Thread(  new Runnable() {  
			  public void run() { myt2.m4t2();   }  
			  }, "Thread2"  ); 
          Thread t3 = new Thread(  new Runnable() {  
			  public void run() { myt2.m4t1();   }  
			  }, "Thread3"  );  //�߳�t1���߳�t3��ͬ����Sychronize_Thread.java
		  t3.start();
          t1.start();
          t2.start();
          
/* Thread3 : 4
Thread3 : 3
Thread3 : 2
Thread3 : 1
Thread3 : 0
Thread2 : 4
Thread2 : 3
Thread2 : 2
Thread2 : 1
Thread2 : 0
Thead1 : 4
Thead1 : 3
Thead1 : 2
Thead1 : 1
Thead1 : 0
          */
          Thread t1 = new Thread(  new Runnable() {  
			  public void run() { myt2.m4t2();   }  //myt2.m4t1();
			  }, "Thead1"  ); 
          Thread t2 = new Thread(  new Runnable() {  
			  public void run() { myt2.m4t2();   }  //myt2.m4t1();
			  }, "Thread2"  ); 
          Thread t3 = new Thread(  new Runnable() {  
			  public void run() { Synchronize_Thread4.m4t3();   } 
			  }, "Thread3"  ); 
          t1.start();
          t2.start();
          t3.start();
          /*
*�ǻ��ⷽ�����public void m4ts(){} ����synchornized(this){}������ƣ�ÿ�β�ͬ
Thead1 : 4
Thread2 : 4
Thead1 : 3
Thread2 : 3
Thread2 : 2
Thead1 : 2
Thread2 : 1
Thead1 : 1
Thread2 : 0
Thead1 : 0
*���ⷽ�����public synchronized void m4ts(){}����󻥳���synchronized(this){}
Thead1 : 4
Thead1 : 3
Thead1 : 2
Thead1 : 1
Thead1 : 0
Thread2 : 4
Thread2 : 3
Thread2 : 2
Thread2 : 1
Thread2 : 0
           */
     }
}